import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import vuexPersist from "vuex-persist"

export default new Vuex.Store({
  state: {
    // 把name保存到vuex里
    name: "",
    shopadd:[],
    shoplike:[],
  },
  mutations: {
    setuser(state,val){
      state.name = val;
    },
    remove(state){
      state.name = "";
    },
    // 添加购物车
    add(state,item){
      var flag=false
      state.shopadd.forEach((res)=>{
        if(res.product_id == item.product_id){
          res.num++
          flag=ture
        }
      });
      if(flag==false){
        Vue.set(item,"num",1)
        state.shopadd.push(item)
      }
    },
    // 添加收藏
    like(state,item){
      var flag = false
      state.shoplike.forEach((res)=>{
        if(res.product_id == item.product_id){
          flag = ture
        }
      });
      if(flag == false){
        state.shoplike.push(item)
      }
    },
    exit(state,id){
      state.shopadd.forEach((res,index)=>{
        if(res.product_id==id){
          state.shopadd.splice(index,1)
        }
      })
    },
    out(state,item){
      state.shoplike.forEach((res,index)=>{
        if(res.product_id == item.product_id){
          state.shoplike.splice(index,1)
        }
      })
    },
    step(){}
  },
  actions: {
  },
  modules: {
  },
  plugins:[new vuexPersist({
    storage:window.localStorage,
  }).plugin,]
})
